package application;

public class Reader
{
	Satellite getSatelliteFromTLE(String tle)
	{
		Satellite sat = new Satellite();

		final double radiansPerDegree = 0.174532925E-1; // Radians pro Grad
		final double minPerDay = 1440.0; // Minuten pro Tag

		String[] lines = tle.split("\n");
		String tleL1 = lines[0];
		String tleL2 = lines[1];
		int lineNumber1 = Integer.parseInt(tleL1.substring(0, 1));
		sat.satelliteNumber = Integer.parseInt(tleL1.substring(2, 7));
		sat.classification = tleL1.charAt(7);
		sat.launchYear = Integer.parseInt(tleL1.substring(9, 11));
		sat.launchNumber = Integer.parseInt(tleL1.substring(11, 14));
		sat.launchPiece = tleL1.substring(14, 17);
		sat.epochYear = Integer.parseInt(tleL1.substring(18, 20));
		sat.epochDayFraction = Double.parseDouble(tleL1.substring(20, 32));
		sat.firstTimeDerivativeOfMeanMotion_divByTwo = Double.parseDouble(tleL1.substring(33, 43));
		double secondTimeDerivativeOfMeanMotion_divBySix = 1.0E-5 * Double.parseDouble(tleL1.substring(44, 50));
		int exponent1 = -Integer.parseInt(tleL1.substring(51, 52));
		sat.secondTimeDerivativeOfMeanMotion_divBySix = secondTimeDerivativeOfMeanMotion_divBySix * Math.pow(10, exponent1);
		double bstarDragTerm = 1.0E-5 * Double.parseDouble(tleL1.substring(54, 59));
		int exponent2 = -Integer.parseInt(tleL1.substring(60, 61));
		sat.bstarDragTerm = bstarDragTerm * Math.pow(10, exponent2);
		sat.ephemerisType = Integer.parseInt(tleL1.substring(62, 63));
		sat.elementSetNumber = Integer.parseInt(tleL1.substring(64, 68).trim());
		int checkSumL1 = Integer.parseInt(tleL1.substring(68, 69));

		int lineNumber2 = Integer.parseInt(tleL2.substring(0, 1));
		int satelliteNumber2 = Integer.parseInt(tleL2.substring(2, 7));
		double inclination = Double.parseDouble(tleL2.substring(8, 16)); // [Grad]
		sat.inclination = inclination * radiansPerDegree;
		double rightAscension = Double.parseDouble(tleL2.substring(17, 25)); // [Grad]
		sat.rightAscension = rightAscension * radiansPerDegree;
		double eccentricity = Double.parseDouble(tleL2.substring(26, 33));
		sat.eccentricity = eccentricity * 1.0E-7;
		double argumentOfPerigee = Double.parseDouble(tleL2.substring(34, 42)); // [Grad]
		sat.argumentOfPerigee = argumentOfPerigee * radiansPerDegree;
		double meanAnomaly = Double.parseDouble(tleL2.substring(43, 51)); // [Grad]
		sat.meanAnomaly = meanAnomaly * radiansPerDegree;
		sat.meanMotion = Double.parseDouble(tleL2.substring(52, 63)); // [U/min]
		sat.revolutionNumberAtEpoch = Integer.parseInt(tleL2.substring(63, 68).trim()); // [Umdrehungen]
		int checksum2 = Integer.parseInt(tleL2.substring(68, 69));

		sat.meanMotion *= ((2 * Math.PI) / (minPerDay));
		sat.firstTimeDerivativeOfMeanMotion_divByTwo *= ((2 * Math.PI) / (minPerDay * minPerDay));
		sat.secondTimeDerivativeOfMeanMotion_divBySix *= ((2 * Math.PI) / (minPerDay * minPerDay * minPerDay));

		sat.calculateDate();
		return sat;
	}
}
